#include "scene.h"

namespace world {

Scene::Scene(int id, const std::string& name)
    : id_(id)
    , name_(name)
{
    // 初始化空间分区
    spatial_partition_ = std::make_unique<SpatialPartition>(100, 100, 10);
}

Scene::~Scene()
{
}

int Scene::get_id() const
{
    return id_;
}

std::string Scene::get_name() const
{
    return name_;
}

void Scene::update()
{
    // 更新场景中的所有实体
    for (auto& pair : entities_) {
        pair.second->update();
    }
}

bool Scene::add_entity(std::shared_ptr<Entity> entity)
{
    if (!entity)
        return false;
    
    entities_[entity->get_id()] = entity;
    
    // 将实体添加到空间分区中
    spatial_partition_->add_entity(entity);
    
    return true;
}

void Scene::remove_entity(int entity_id)
{
    auto it = entities_.find(entity_id);
    if (it != entities_.end()) {
        // 从空间分区中移除实体
        spatial_partition_->remove_entity(it->second);
        
        entities_.erase(it);
    }
}

std::shared_ptr<Entity> Scene::get_entity(int entity_id) const
{
    auto it = entities_.find(entity_id);
    if (it != entities_.end())
        return it->second;
    
    return nullptr;
}

std::vector<std::shared_ptr<Entity>> Scene::get_entities_in_range(const Position& pos, float range) const
{
    return spatial_partition_->get_entities_in_range(pos, range);
}

} // namespace world    